<?php
  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
	<script type="text/JavaScript" src="js/validate.js"></script>
	<script type="text/JavaScript">
	<!--
		function ValidateFields() 
		{
	        	return ValidateFieldNotEmpty("changePassForm", "password", "password") && 
ValidateFieldNotEmpty("changePassForm", "passwordconfirm", "password confirmation") && ValidateFieldsMatch("changePassForm", "password", "passwordconfirm", "The password confirmation does not match the given password");
        	}
	//-->
	</script>
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
	function changePass()
	{
		$db = new Connect();
		$pass      = $_POST['password'];
		$pconfirm  = $_POST['passwordconfirm'];
	
		$sql = "UPDATE Guest SET password ='" . $pass . "' WHERE email=(SELECT email FROM MemberInfo WHERE mID = " . $_SESSION['mid'] . ")";
		//echo $sql;

		$result = $db->query($sql);
		if(!$result)
		{
			return "failed";
		}
		else
		{
			return "success";
		}
	}

    ?>
    <div id="main">
	<?php
		if(isset($_POST['password']))
	        {
        	        $result = changePass();
               		if($result == "failed")
                	{ 
                       		echo "You failed to reset your password";
                	}
                	else
                	{
                        	echo "<b>You have changed successfully</b>";
                	}
        	}

	?> 
	<form id="changePassForm" action="" onsubmit="return ValidateFields();" method="post">
                        <table border="0">
                                <tr>
                                        <td class="normal">Change new password</td>
					<td class="normal"><input type="password" name="password"></td>
                                </tr>
				<tr>
                                        <td class="normal">Confirm new password</td>
                                        <td class="normal"><input type="password" name="passwordconfirm"></td>
                                </tr>

			</table>
			<p></p>
                        <input type="submit" class="largeButton" name="changePass" value="Change"/>
	</form>
    </div>
</body>
</html>
